package com.cst.intercepterdemo.chain.node;

import com.cst.intercepterdemo.context.IInterceptContext;
import com.cst.intercepterdemo.intercepter.IRequestIntercepter;

/**
 * <ul>
 * <li>文件名称 : com.cst.intercepterdemo.chain.node.AbstractIntercepterChainNode
 * </li>
 * <li>版权所有 : 版权所有(C)2016-2020</li>
 * <li>公 司 : CST</li>
 * <li>创建时间 : 2016/12/2</li>
 * <li>修改记录 : </li>
 * </ul>
 * 类说明：责任链节点抽象类
 *
 * @author Allen
 * @version 1.0
 */
public abstract class AbstractIntercepterChainNode implements IIntercepterChainNode {

    public AbstractIntercepterChainNode() {
    }

    public AbstractIntercepterChainNode(IIntercepterChainNode next) {
        this.next = next;
    }

    private IIntercepterChainNode next;

    @Override
    public boolean dochain(IInterceptContext context){
        if(intercept(context)){
            if(hasNext()){
                return next.dochain(context);
            }
            return true;
        }
        return false;
    }

    protected abstract boolean intercept(IInterceptContext context);

    @Override
    public IIntercepterChainNode next(){
        return next;
    }

    @Override
    public boolean hasNext(){
        return next != null;
    }

    @Override
    public void setNext(IIntercepterChainNode next) {
        this.next = next;
    }
}
